package com.wonders.hs.pd.mm.web;

import com.corundumstudio.socketio.Configuration;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.annotation.SpringAnnotationScanner;
import com.wonders.hs.pd.mm.web.config.SystemConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;

import java.util.Date;

/**
 * @author banglong
 * @date 2016/8/17
 */
@SpringBootApplication
public class Application extends SpringBootServletInitializer {

  private static final Logger logger = LoggerFactory.getLogger(Application.class);

//    @Autowired
//    private ApplicationContext applicationContext;

//    @PostConstruct
//    public void init() {
////        ApplicationContextUtil.setApplicationContext(applicationContext);
//        // 执行定时任务 更新 物品缓存
////        GoodsCatchUtils.executeBuildGoodsCatch();
//    }

  // socket io
  @Bean
  public SocketIOServer socketIOServer() {
    Configuration configuration = new Configuration();
    configuration.setHostname(SystemConfig.SOCKETIO_HOST);
    configuration.setPort(Integer.parseInt(SystemConfig.SOCKETIO_PORT));

    // 该处可以用来进行身份验证
    configuration.setAuthorizationListener(handshakeData -> true);
    return new SocketIOServer(configuration);
  }

  @Bean
  public SpringAnnotationScanner springAnnotationScanner(SocketIOServer socketIOServer) {
    return new SpringAnnotationScanner(socketIOServer);
  }

  @Override
  protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(Application.class);
  }

  public static void main(String[] args) throws Exception {
    SpringApplication.run(Application.class, args);
  }
}
